package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeFile;
import com.ruoyi.food.mapper.OpmFoodSafeFileMapper;
import com.ruoyi.food.service.IOpmFoodSafeFileService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 园区食品安全档案Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeFileServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeFileService {
    @Autowired
    private OpmFoodSafeFileMapper opmFoodSafeFileMapper;

    /**
     * 查询园区食品安全档案
     *
     * @param id 园区食品安全档案ID
     * @return 园区食品安全档案
     */
    @Override
    public OpmFoodSafeFile selectOpmFoodSafeFileById(Long id) {
        OpmFoodSafeFile result = opmFoodSafeFileMapper.selectOpmFoodSafeFileById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询园区食品安全档案列表
     *
     * @param opmFoodSafeFile 园区食品安全档案
     * @return 园区食品安全档案
     */
    @Override
    public List<OpmFoodSafeFile> selectOpmFoodSafeFileList(OpmFoodSafeFile opmFoodSafeFile) {
        return opmFoodSafeFileMapper.selectOpmFoodSafeFileList(opmFoodSafeFile);
    }

    /**
     * 新增园区食品安全档案
     *
     * @param opmFoodSafeFile 园区食品安全档案
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeFile(OpmFoodSafeFile opmFoodSafeFile) {
        opmFoodSafeFile.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeFile.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeFile.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeFile.getFileIdList(), opmFoodSafeFile.getId().toString());
        return opmFoodSafeFileMapper.insertOpmFoodSafeFile(opmFoodSafeFile);
    }

    /**
     * 修改园区食品安全档案
     *
     * @param opmFoodSafeFile 园区食品安全档案
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeFile(OpmFoodSafeFile opmFoodSafeFile) {
        opmFoodSafeFile.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeFile.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeFile.getFileIdList(), opmFoodSafeFile.getId().toString());
        return opmFoodSafeFileMapper.updateOpmFoodSafeFile(opmFoodSafeFile);
    }

    /**
     * 批量删除园区食品安全档案
     *
     * @param ids 需要删除的园区食品安全档案ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeFileByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeFileMapper.deleteOpmFoodSafeFileByIds(ids);
    }

    /**
     * 删除园区食品安全档案信息
     *
     * @param id 园区食品安全档案ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeFileById(Long id) {
        return opmFoodSafeFileMapper.deleteOpmFoodSafeFileById(id);
    }
}
